<template>
  <view>
    <view class="search-box">
      <uni-search-bar cancelButton="none"  @input="input" :radius="100" placeholder="请输入搜索内容"></uni-search-bar>
    </view> 
    <!-- 搜索建议列表 -->
    <view class="searchlist-box" v-if="searchList.length !=0">
      <view class="searchlist-item" v-for="(item,i) in searchList" :key="i">
        <view class="searchlist-name" @click="gotoGoodList(item)">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="15"></uni-icons>
        </view>
      </view>
      <!-- 搜索历史模块 -->
      <view class="history-box" v-else>
        <view class="history-title">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="clean"></uni-icons>
        </view>
        <!-- 列表区域 -->
        <view class="history-list" >
          <uni-tag  :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoods(item)"></uni-tag>
          
        </view>
      </view>
      
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,        
        kw:'', // 存储搜索关键词
        searchList:[], //搜索数据列表
        historyList:[]
        
      };
    },
    onLoad(){
      this.historyList = JSON.parse(uni.getStorageSync('kw')||'[]')
    },
    computed:{
      // 翻转数组将数据放在头部区域
      histories(){
        return [...this.historyList].reverse()
      }
    },
    methods:{
      // 监听input事件,利用防抖提高搜索性能
      input(e){
        clearTimeout(this.timer)
      this.timer = setTimeout(()=> {
          this.kw=e
          // console.log(this.kw)
          this.getSearchList()
        }, 500);

      },
      //获取搜索数据
    async getSearchList(){
        if(this.kw=='') {
          this.searchList=[]
          return
        }
        const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
        if(res.meta.status!==200) return uni.$showMsg()
        this.searchList=res.message
        this.saveHistoryList()
      },
      gotoGoodList(item){
        // console.log(item.goods_id)
        uni.navigateTo({
          url:"/subpkg/goods_list/goods_list?goods_id="+item.goods_id
        })
      },
      saveHistoryList(){
        // this.historyList.push(this.kw)
        const set= new Set(this.histories)
        set.delete(this.kw)
        set.add(this.kw)
        // console.log(set)
        this.historyList=Array.from(set)
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      clean(){
        this.historyList=[]
        uni.setStorageSync('[]')
      },
      gotoGoods(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+kw
        })
      }
      
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
  background-color: #c00000;
}
.searchlist-box {
  padding: 0 5px;
  .searchlist-item{
    display: flex;
    justify-content: space-between;
    padding: 13px 0;
    font-size: 12px;
    border: 1px solid #efefef;
    align-items: center;
    .searchlist-name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-left: 3px;
    }
  }
}
.history-box {
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 12px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: nowrap;
   .uni-tag {
     margin-top: 5px;
     margin-right: 5px;
   }
    
  }
}

</style>
